package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;

public class _365jiaNewsAnalyse implements AnalyseNews {
//http://365jia.cn/news/2014-02-12/F44200D408CB0910.html

	private static final String[] regex={
		"http://.*365jia.cn/news/[0-9]+-[0-9]+-[0-9]+/[0-9A-Z]*[_0-9]*.html",
		"http://365jia.cn/news/images/[0-9]*-[0-9]*-[0-9]*/[0-9A-Z]*.html",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
//		NewsMeta news=new ParseNewsUtils().parse(urlMeta);
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		title=doc.select("h1.nt_title").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("h1.news_title").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("h2.tle_pic").text();
			}
		}
		if(StringUtils.isBlank(title)){
			title=doc.select(".nt_title").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.nt_cont_txt"));
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select("div.news_body"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.mp_title_txt"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.mp_details_text"));
		}

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");

		date=DateUtils.matchDate(doc.select(".nt_title_bar,p.news_meta,div.edit_info").text());
		Integer commentNum = NumberUtils.matchNumber(doc.select("#pv").text());
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		news.setCommentNum(commentNum);
		return news;
	}

	public static void main(String[] args) {
		String url="http://365jia.cn/news/2016-11-07/198F400EE01C960C.html";
		_365jiaNewsAnalyse test=new _365jiaNewsAnalyse();
		System.out.println(test.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
